package pl.prv.polanie.client.widgets.schedule;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dto.CSSubjectEventDTO;
import pl.prv.polanie.client.dto.CSVSubjectEventDTO;
import pl.prv.polanie.client.model.CacheService;
import pl.prv.polanie.client.model.ContextSettings;
import pl.prv.polanie.client.model.ViewDataProvider;
import pl.prv.polanie.client.widgets.layout.tooltip.ToolTip;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.Widget;

/**
 * Klasa uzywana do pokazywania na podziale zajec na przyklad zajec prowadzonych przez wykladowce, ktory prowadzi przeciagane zajecia.
 * @author Janusz Prokulewicz
 * Created: Jun 4, 2008
 */

public class SubjectEventHint extends Composite {
	
	private static final String STYLE_EVENT = "my-Event-Hint";
	private static final String STYLE_EVENT_BOTTOM_LEFT = "my-Event-Hint-bottomLeft";
	private static final String STYLE_EVENT_TOP_RIGHT = "my-Event-Hint-topRight";
	
	//LECTURER HINT
	private static final String STYLE_EVENT_LECTURER_HINT = "my-Event-Hint";
	private static final String STYLE_EVENT_LECTURER_HINT_BOTTOM_LEFT = "my-Event-LecturerHint-bottomLeft";
	private static final String STYLE_EVENT_LECTURER_HINT_TOP_RIGHT = "my-Event-LecturerHint-topRight";
	

	private static final String STYLE_EVENT_BACKGROUND_IMAGE = "my-Event-Hint-backgroundImage";
	
	
	private static final String STYLE_EVENT_HEADER = "my-Event-header";
	private static final String STYLE_EVENT_HANDLER = "my-Event-handler";
	
	private static final String STYLE_EVENT_PROPERTY = "my-Event-property";
	private static final String STYLE_EVENT_PROPERTY_RIGHT = "my-Event-property-right";
	private static final String STYLE_EVENT_DETAILS = "my-Event-details";
	
	
	
	/** Wzkazuje, jaki rodzaj podpowiedzia jest ten obiekt. Dostepne wartosci nizej.  */
	private int hintSort = HINT_SORT__NOT_SPECIFIED;

	public static final int HINT_SORT__NOT_SPECIFIED = 0;
	public static final int HINT_SORT__ROOM = 1;
	public static final int HINT_SORT__LECTURER = 2;
	public static final int HINT_SORT__GROUP = 3;

	
	
	/** html with triangle/rectangular image */
	private final Image backgroundImage = new Image();
	
	//user can catch for it and start dragging
	private final HTML handlerHTML = new HTML("");
	
	
	//private static final HTML 
	
	private boolean wasAttached = false;
	
	/**
	 * wskazuje czy zajecia odbywaja sie:
	 * 	co tydzien = 0
	 * 	w tygodnie parzyste = 1
	 *  w tygodnie nieparzyste = 2
	 */ 
//	private int allOrEvenOrOdd = 0;
	
	
	private AbsolutePanel mainAP = new AbsolutePanel();
	
	private CSSubjectEventDTO csSubjectEventDTO = null;
	private CSVSubjectEventDTO csvSubjectEventDTO = null;
	
	private HTML eventProperty1HTML;  
	private HTML eventProperty2HTML;  
	private HTML eventProperty3HTML;
	
	//wspolrzedna subject eventa na formatce dnia 
	private int y; 
	
	
	private MouseListener mouseListenerAdapter = new MouseListenerAdapter(){
		public void onMouseMove(Widget sender, int x, int y) {
//			Planner.log("onMouseMove");
			if (!Planner.isToolTipDisplayed()) {
				ToolTip tip = new ToolTip(csvSubjectEventDTO);
				Planner.displayToolTip(tip, x + sender.getAbsoluteLeft() + 10, y + sender.getAbsoluteTop() + 10);	
			} else {
				Planner.setToolTipPosition(x + sender.getAbsoluteLeft() + 10, y + sender.getAbsoluteTop() + 10);
			}
		}
		public void onMouseLeave(Widget sender) {
			Planner.hideToolTip();
//			Planner.log("handler.onMoulseLeave");
		}
	};
	
	public SubjectEventHint(CSSubjectEventDTO dto, int hintSort) {
		this.hintSort = hintSort;
		this.csSubjectEventDTO = dto;
		this.y = ScheduleUtil.convertFromStartTimeToPixels(dto.getStartTime().intValue(), CacheService.isFullTimeSemesterSelected());
		
		CSVSubjectEventDTO csvSE = ViewDataProvider.getCSVSubjectEvent(dto);
		this.csvSubjectEventDTO = csvSE;
		setupEventPropertiesHTML(csvSE);
		setupBackgroundImageHTML();
		setupHandlerHTML();
		setupMainAP();

		int height = ScheduleUtil.convertFromDurationToPixels(dto.getDuration().intValue(), CacheService.isFullTimeSemesterSelected());
//		Planner.log("LecturerPreferences.kostr() duration: " + dto.getDuration().intValue() + ", height " + height);
		setHeight(height + "px");

		
		initWidget(mainAP);
		
		switch (hintSort) {
		case HINT_SORT__NOT_SPECIFIED:
			setStyleName(STYLE_EVENT);
			break;
		case HINT_SORT__LECTURER:
		case HINT_SORT__ROOM:
		case HINT_SORT__GROUP:
			setStyleName(STYLE_EVENT_LECTURER_HINT);
			break;
		default:
			break;
		}
//		Borders.addGrayDashedBorders(mainAP);
	}


	protected void onAttach() {
		// TODO Auto-generated method stub
		super.onAttach();
		if (!wasAttached) {
			wasAttached = true;
			
//			if (subjectEventDTO != null) {
			if (csSubjectEventDTO != null) {
				int eventSort = csSubjectEventDTO.getEventSort();
				if (eventSort == 0) {
					setAll();
				} else if (eventSort == 1) {
					//parzysty
					setBottomLeft();
				} else if (eventSort == 2) {
					//nieparzysty
					setTopRight();
				}
			}
		}
		//attached = true;
	}

	private void setupEventPropertiesHTML(CSVSubjectEventDTO csvSE) {
		if (ContextSettings.isGuiDebugMode()) {
			eventProperty1HTML = new HTML("[" + csvSE.getId() + "]" + csvSE.getSubjectNameSymbol());
		} else {
			eventProperty1HTML = new HTML(csvSE.getSubjectNameSymbol());
		}
		eventProperty1HTML.setStyleName(STYLE_EVENT_PROPERTY);
		eventProperty2HTML = new HTML(csvSE.getSubjectTypeInitial() + " " + csvSE.getRoom());
		eventProperty2HTML.setStyleName(STYLE_EVENT_PROPERTY);
		
		String lecturerSurname = csvSE.getLecturerSurname(); 
		if (lecturerSurname.equals("-no lecturer-")) {
			lecturerSurname = "";
		}
		eventProperty3HTML = new HTML(lecturerSurname);
//		Planner.log("SubjectEventHint.setupEventPropertiesHTML: " + " WSTAWIAM LECTURERA AAA  " + lecturerSurname);
		eventProperty3HTML.setStyleName(STYLE_EVENT_PROPERTY);
	}
	
	private void setupBackgroundImageHTML() {
		backgroundImage.setStyleName(STYLE_EVENT_BACKGROUND_IMAGE);
		backgroundImage.setWidth(200+"px");
		backgroundImage.setHeight(54+"px");
	}
	
	private void setupHandlerHTML() {
		handlerHTML.setStyleName(STYLE_EVENT_HANDLER);
		handlerHTML.setWidth(200+"px");
		handlerHTML.setHeight(54+"px");
		handlerHTML.addMouseListener(mouseListenerAdapter);
//		handlerHTML.addClickListener(clickListenerAdapter);
	}
	
	private void setAll() {
		if (csSubjectEventDTO != null) {
			csSubjectEventDTO.setEventSort(0);
		}
		removeStyleName(STYLE_EVENT_TOP_RIGHT);
		removeStyleName(STYLE_EVENT_BOTTOM_LEFT);
		
		switch (hintSort) {
		case HINT_SORT__NOT_SPECIFIED:
			backgroundImage.setUrl("./images-schedule/gray_all.png");
			break;
		case HINT_SORT__LECTURER:
			backgroundImage.setUrl("./images-schedule/pink_all.png");
			break;
		case HINT_SORT__ROOM:
		case HINT_SORT__GROUP:
			backgroundImage.setUrl("./images-schedule/yellow_all.png");
			break;
		default:
			break;
		}
		
		mainAP.setWidgetPosition(eventProperty1HTML, 0, 0);
		mainAP.setWidgetPosition(eventProperty2HTML, 0, 10);
		mainAP.setWidgetPosition(eventProperty3HTML, 0, 20);
	}
	
	private void setTopRight() {
		if (csSubjectEventDTO != null) {
			csSubjectEventDTO.setEventSort(2);
		}
		removeStyleName(STYLE_EVENT_BOTTOM_LEFT);
		addStyleName(STYLE_EVENT_TOP_RIGHT);
		
		switch (hintSort) {
		case HINT_SORT__NOT_SPECIFIED:
			backgroundImage.setUrl("./images-schedule/gray_top_right_200_54.png");
			break;
		case HINT_SORT__LECTURER:
			backgroundImage.setUrl("./images-schedule/pink_top_right_200_54.png");
			break;
		case HINT_SORT__ROOM:
		case HINT_SORT__GROUP:
			backgroundImage.setUrl("./images-schedule/yellow_top_right_200_54.png");
			break;
		default:
			break;
		}
		
		
		mainAP.setWidgetPosition(eventProperty1HTML, mainAP.getOffsetWidth()-eventProperty1HTML.getOffsetWidth()-2, 0);
		mainAP.setWidgetPosition(eventProperty2HTML, mainAP.getOffsetWidth()-eventProperty2HTML.getOffsetWidth()-2, 10);
		mainAP.setWidgetPosition(eventProperty3HTML, mainAP.getOffsetWidth()-eventProperty3HTML.getOffsetWidth()-2, 20);
	}
	
	private void setBottomLeft() {
		if (csSubjectEventDTO != null) {
			csSubjectEventDTO.setEventSort(1);
		}
		removeStyleName(STYLE_EVENT_TOP_RIGHT);
		addStyleName(STYLE_EVENT_BOTTOM_LEFT);
		
		
		switch (hintSort) {
		case HINT_SORT__NOT_SPECIFIED:
			backgroundImage.setUrl("./images-schedule/gray_bottom_left_200_54.png");
			break;
		case HINT_SORT__LECTURER:
			backgroundImage.setUrl("./images-schedule/pink_bottom_left_200_54.png");
			break;
		case HINT_SORT__ROOM:
		case HINT_SORT__GROUP:
			backgroundImage.setUrl("./images-schedule/yellow_bottom_left_200_54.png");
			break;
		default:
			break;
		}
		
		mainAP.setWidgetPosition(eventProperty1HTML, 0, mainAP.getOffsetHeight()-30);
		mainAP.setWidgetPosition(eventProperty2HTML, 0, mainAP.getOffsetHeight()-20);
		mainAP.setWidgetPosition(eventProperty3HTML, 0, mainAP.getOffsetHeight()-10);
	}
	
	private void setupMainAP() {
		mainAP.setWidth("200px");
//		mainAP.setHeight("54px");
		
		mainAP.add(eventProperty1HTML,-1,-1);
		mainAP.add(eventProperty2HTML,-1,-1);
		mainAP.add(eventProperty3HTML,-1,-1);
		
		//mainAP.add(eventDetailsHTML);
		mainAP.add(backgroundImage, 0, 0);
		mainAP.add(handlerHTML, 0, 0);
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
//	@Override
	public void setHeight(String height) {
		mainAP.setHeight(height);
		backgroundImage.setHeight(height);
		handlerHTML.setHeight(height);
	}
	
	public void setWidth(String width) {
		super.setWidth(width);
		backgroundImage.setWidth(width);
		handlerHTML.setWidth(width);
		
		if (csSubjectEventDTO != null && csSubjectEventDTO.getEventSort() == 2) {
			setTopRight();
		}
	}



	public int getHintSort() {
		return hintSort;
	}
	
	

}
